package com.dsg.workflow;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dsg.common.util.SingletonLoginUtils;
import com.dsg.common.util.toolbox.StringUtil;
import com.dsg.entity.account.DsgAccountCard;
import com.dsg.entity.account.DsgAccountTrade;
import com.dsg.entity.approve.DsgApproveCase;
import com.dsg.service.account.IDsgAccountCardService;
import com.dsg.service.account.IDsgAccountTradeService;
import com.dsg.util.RRException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 交易賬目
 *
 * @author rong
 */
@Service
public class WorkflowService20 extends AbstractWorkflowService {

    @Autowired
    private IDsgAccountTradeService dsgAccountTradeService;
    @Autowired
    private IDsgAccountCardService dsgAccountCardService;

    @Override
    public void approveFillBack(DsgApproveCase approveCase) {
        //查詢申請單
        QueryWrapper<DsgAccountTrade> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("APP_NO", approveCase.getAppNo());
        queryWrapper.eq("ENT_CODE", SingletonLoginUtils.getSystemEntCode());
        DsgAccountTrade dsgAccountTrade = dsgAccountTradeService.getOne(queryWrapper, false);
        if (dsgAccountTrade != null) {
            //已審批
            if (DsgApproveCase.Status.approved.getCode().intValue() == approveCase.getStatus().intValue()) {
                DsgAccountCard dsgAccountCard = dsgAccountCardService.getAddDsgAccountCard(dsgAccountTrade.getCardNo());
                if (dsgAccountCard != null) {
                    BigDecimal arrearsValue = dsgAccountCard.getArrearsValue();
                    dsgAccountTrade.setDataStatus(DsgAccountTrade.DATA_STATUS_1);
                    dsgAccountTrade.setPreDept(arrearsValue);
                    //總結欠
                    if (StringUtil.isNotEmpty(dsgAccountTrade.getInOut())) {
                        switch (dsgAccountTrade.getInOut()) {
                            case DsgAccountTrade.IN_OUT_INCOME:
                                arrearsValue = arrearsValue.add(dsgAccountTrade.getTraAmount());
                                break;
                            case DsgAccountTrade.IN_OUT_EXPENDITURE:
                                arrearsValue = arrearsValue.subtract(dsgAccountTrade.getTraAmount());
                                break;
                            default:
                                throw new RRException("收入/支出類型系統參數不正確");
                        }
                    }
                    dsgAccountTrade.setAftDept(arrearsValue);
                    dsgAccountCard.setArrearsValue(arrearsValue);
                    dsgAccountCard.setWaitPostValue(dsgAccountCard.getWaitPostValue().add(dsgAccountTrade.getTraAmount()));
                    dsgAccountCard.setTradeId(dsgAccountTrade.getOid());
                    dsgAccountCardService.updateById(dsgAccountCard);
                }
            } else if (DsgApproveCase.Status.undo.getCode().intValue() == approveCase.getStatus().intValue()) {
                dsgAccountTrade.setDataStatus(DsgAccountTrade.DATA_STATUS_3);
            }
            dsgAccountTradeService.updateById(dsgAccountTrade);
        }

    }
}
